<?php
//error_reporting(E_ALL ^E_NOTICE);
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$_MAMBOTS->registerFunction( 'onPayProsessStart', 'jeProcessIveryStart' );
$_MAMBOTS->registerFunction( 'onPayProsessFinish', 'jeProcessIveryFinish' );

function jeProcessIveryFinish()
{
    $processor = mosGetParam( $_REQUEST, 'processor'); // success or fail
    
    if($processor != '.ivery') return false;
    
    $order['id'] = mosGetParam( $_REQUEST, 'order_id');
    $order['pay'] = mosGetParam( $_REQUEST, 'pay');
    $order['gateway'] = $processor;
    $order['gateway_id'] = mosGetParam( $_REQUEST, 'Lite_TransactionIndex');
    $order['price'] = mosGetParam( $_REQUEST, 'amount');
    
    return $order;
}

function jeProcessIveryStart( $params )
{
    Global $my, $database, $mosConfig_live_site;
    
    if($params['processor'] != '.ivery') return;
    
    $query = "SELECT id"
    . "\n FROM #__mambots"
    . "\n WHERE element = 'joomlaequipment.payprocessor{$params[processor]}'"
    . "\n AND folder = 'system'"
    ;
    $database->setQuery( $query );
    $id     = $database->loadResult();
    $mambot = new mosMambot( $database );
    $mambot->load( $id );
    $botParams = new mosParameters( $mambot->params );
    
    $param['option']    = mosGetParam( $_REQUEST, 'option');
    $param['task']      = 'pay';
    $param['processor'] = '.optimal';
    $param['order_id']  = $params['order_id'];
    $param['amount']    = $params['order_amount'];
    $return = urlencode( $mosConfig_live_site . '/index.php?' . 
        jpArray2Url($param) . '&pay=success');
    $cancel_return = urlencode( $mosConfig_live_site . '/index.php?' . 
        jpArray2Url($param) . '&pay=fail');
    
    //unset($param);
    
    //$param['ACCOUNTID'] = $botParams->def( 'accountid');
    $param['Lite_Merchant_ApplicationId']= $botParams->def( 'merchantid');
    
    //$param['CUSTNAME']  = $my->name;
    //$param['CUSTMAIL']  = $my->email;
 
    $param['Lite_Order_LineItems_Product_1']  = $params['order_title'];
    $param['Lite_Order_LineItems_Quantity_1']  = 1;
    $param['Lite_Order_LineItems_Amount_1']  = $params['order_amount'];
    $param['Lite_On_Error_Resume_Next']  = 'True';
    
    $param['Lite_ConsumerOrderID_PreFix'] = $params['order_id'];
    $param['Lite_Order_Amount']    = $params['order_amount'];
    $param['Lite_Order_Terminal']    = 5;
    
    $param['Lite_Website_Successful_url'] = 'http://jdev10';//$return;
    $param['Lite_Website_Fail_url']   = 'http://jdev10';//$cancel_return;
    $param['Lite_Website_TryLater_url']   = 'http://jdev10';//$cancel_return;
    $param['Lite_Website_Error_url']   = 'http://jdev10';//$cancel_return;

    $param['currentMonth']  = date("m");
    $param['currentYear']   = date("Y");
    $param['paymentmethod'] = 'Credit card';

    
    if(!$param['Lite_Merchant_ApplicationId'] || !$param['Lite_Order_Amount'] || !$param['Lite_ConsumerOrderID_PreFix'])
    {
        echo "<P>Error Payment Processing: some of the params passed are empty ($param[Lite_Merchant_ApplicationId] || !$param[Lite_Order_Amount] || !$param[Lite_ConsumerOrderID_PreFix])</P>";
        return;
    }
    
    $url = 'https://backoffice.iveri.co.za/Lite/Authorise.asp?' . 
        jpArray2Url($param);
    echo $url;
    header("location: $url");
}

if(!defined('JP_IMPLODE'))
{
    define('JP_IMPLODE', 1);
    function jpArray2Url($param)
    {
        foreach($param AS $k => $v)
        {
            $out[] = "$k=$v";
        }
        return implode('&', $out );
    }
}



?>
